<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link rel="stylesheet" href="../gameStyle.css" type="text/css" />
        <script type="text/javascript" src="http://lib.ivank.net/ivank.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript" src="types.js"></script>
        <script type="text/javascript" src="button.js"></script>
        <script type="text/javascript" src="timer.js"></script>
        <script type="text/javascript" src="level.js"></script>
    </head>
    <body onload="Start();">

        <canvas id ="canvas" width="750" height="500"></canvas>
        <script>            
            var canvas = document.getElementById("canvas");
            var cwidth = canvas.getAttribute("width");
            var cheight = canvas.getAttribute("height");
            
            var oznacenie = false;
            var oznaceny;
            function Start(){
                var stage = new Stage("canvas");
                var level = new CircuitLevel(5,typePhysics);
                level.init();
                stage.addChild(level,0); // pridavam level na index 0, index tam inak nemusim davat
                level.
                    intervalID = window.setInterval(level.run, 1000 / 60);  
            }
        
 
            var solved = false;
            var len = 3;
            var size = 100;
            function Configure(){
                this.configArray = new Array(len);
                this.init = init;
                function init(){
                    for(var i = 0; i < len; i++){
                        var array = new Array(len);  
                        for (var j = 0; j < len; j++){
                            array[j]=i+j;
                        }
                        configArray[i]=array;
                    } 
                }
            }
            
            function Square(i,j,order){   
                Sprite.apply(this);
                this.i = i;
                this.j = j;
                this.oznacene = false;
                this.order = order;
                this.border;
                this.picture;
                this.init = init;
                const IMAGE_URL= "../pictures/obv"+ order +".bmp";
                
                function init(){
                    
                    this.border = new Sprite();		
                    this.border.graphics.beginFill(black);
                    this.border.graphics.drawRect(j*size - 2, i*size - 2, size+4, size+4);
                    this.border.visible = true;
                    this.addChild(this.border);
                    ///
                    var bitmapData = new BitmapData(IMAGE_URL);
                    var bitmap = new Bitmap(bitmapData);
                    bitmap.x = this.j*size;
                    bitmap.y = this.i*size;
//                    if (this.oznacene){          
//                        bitmap.alpha = 0.1;
//                        alert(this.oznacene);
//                    }else{
                        bitmap.alpha = 1;        
//                    }
                    bitmap.scaleX = bitmap.scaleY = 0.4;
                    this.addChild(bitmap); 
                            
                }
                
                
                this.show = show;
                function show(){
                    
                    
                }
                
                this.over = over;
                function over(e){
          
                    e.target.alpha = 0.7;
                }
                
                this.out = out;
                function out(e){
                    if ( e.target.oznacene ){
                       
                        e.target.alpha = 0.1;
                       
                    }else{
                        e.target.alpha = 1;        
                    }
                }
                this.down = down;
                function down(e){
          
                    if (e.target.oznacene){
                        e.target.alpha = 1;
                        e.target.oznacene = false;
                        oznacenie = false;
                        oznaceny = null;
                         
                    }else if(!oznacenie){
                        e.target.alpha = 0.1; 
                        e.target.oznacene = true;
                        oznacenie = true;
                        oznaceny = e.target;
                    }else{
                        oznaceny.alpha=1;
                        oznaceny.oznacene=false;
                        e.target.alpha = 0.1; 
                        e.target.oznacene = true;
                        oznaceny = e.target;
                    }
                }
                
                this.up = up;
                function up(e){
                    // e.target.alpha = 1;
                }
                
                this.addEventListener(MouseEvent.MOUSE_OVER, this.over);
                this.addEventListener(MouseEvent.MOUSE_OUT , this.out);
                this.addEventListener(MouseEvent.MOUSE_DOWN, this.down);
                this.addEventListener(MouseEvent.MOUSE_UP , this.up);
            }
            Square.prototype = new Sprite();
            
            function Playground(x,y){
                this.playgroundArray = new Array(len);             
                Sprite.apply(this);
                this.x = x;
                this.y = y;        
                this.init = init;
                function init(){ 
                    for(var i = 0; i < len; i++){
                        var array = new Array(len);  
                        for (var j = 0; j < len; j++){
                      
                            var square = new Square(i,j,i+(j+2*i));
                                    
                            array[j]= square;
                         
                            
                        }
                        this.playgroundArray[i]=array;
                    }
                  
                }
                
                this.show = show;
                function show(){
                    for(var i = 0; i < len; i++){ 
                        for (var j = 0; j < len; j++){    
                            this.playgroundArray[i][j].init()                  
                            this.addChild(this.playgroundArray[i][j]);           
                        }   
                    }                     
                }
                
                this.vloz = vloz;
                function vloz(sq,i,j){  
                    // this.removeChild(this.playgroundArray[i][j]);
                    sq.i = i;
                    sq.j = j;
                    sq.oznacene = false;
                    oznacenie=false;
                    oznaceny= null;
                    this.playgroundArray[i][j] = sq; 
                    
          
                    
                    // this.playgroundArray[i][j].show();
                    // this.addChild(this.playgroundArray[i][j]); 
                }
                
                this.susedne = susedne;
                function susedne(i1,j1, i2,j2){
                    //alert(i1+" "+j1+" "+i2+" "+j2+" "+len);
                    if ((i1>=0) && (i1< len) &&
                        (i2>=0) && (i2< len) &&
                        (j1>=0) && (j1< len) &&
                        (j2>=0) && (j2< len) ){    
                        
                        if(((i1==i2-1)&&(j1==j2)) ||
                            ((i1==i2+1)&&(j1==j2)) || 
                            ((i1==i2)&&(j1==j2+1)) || 
                            ((i1==i2)&&(j1==j2-1))   ){
                            return true;
                        }   
                    }
                    return false; 
                } 
                 
            
                this.vymena = vymena; 
                function vymena(i,j,volI,volJ){
                    
                    if (this.playgroundArray[volI][volJ].order == 4){
                        var susedne = this.susedne(i,j,volI,volJ);
                        if (susedne){ 
                            //alert(this.playgroundArray[i][j].i+" "+ this.playgroundArray[i][j].j);
                            var pom =  this.playgroundArray[i][j];
                            this.vloz(this.playgroundArray[volI][volJ],i,j);
                            this.vloz(pom,volI,volJ);
                            // alert(this.playgroundArray[i][j].i+" "+ this.playgroundArray[i][j].j);
                        }                      
                    }         
                }
                
                this.kontrola = kontrola;
                function kontrola(){
                    var config = new Configure();                 
                    solved = true;
                    for(var i = 0; i < len; i++){  
                        for (var j = 0; j < len; j++){
                            if(config.configArray[i][j]!=this.playgroundArray[i][j].order) {    
                                solved = false;    
                            } 
                        }
                    }
                }
                
                this.miesanie = miesanie; 
                function miesanie(){
                    for(var i = 0; i < len; i++){
                       
                        for (var j = 0; j < len; j++){
                            var randomI = Math.random(3); 
                            var randomJ = Math.random(3);                           
                            var pom =  this.playgroundArray[i][j];
                            this.vloz(this.playgroundArray[volI][volJ],i,j);
                            this.vloz(pom,volI,volJ); 
                        }  
                    
                    }
                    
                }
                
                
                
            }
            Playground.prototype = new Sprite();
                    
            // LEVEL
            function CircuitLevel(name, level, type){
                Sprite.apply(this); // dedim stage  
                Level.apply(this); // dedim level  
                this.name = name;
                this.level = level;
                this.type = type;
                this.canvas = canvas;
                // toto som pridal
                this.playGround;
                // methodes
                this.init = init;
                this.click = click;
                this.move = move;
                
                // totok som zmenil
                function init(){
                    var x = cwidth/2 - len*size/2;
                    var y = cheight/2 - len*size/2;
                    this.playGround = new Playground(x,y);
                    this.playGround.init(); 
                    this.playGround.vymena(0, 1, 1, 1);
                    this.playGround.show();
                    this.addChild(this.playGround);
                    
                }
                
                function click(e){
                    // TODO
                    // e je typu event
                }
                
                function move(e){
                    // TODO
                }
                
                // toto neviem ako funguje presne, ale mozes to pridat aj
                // ku akemukolvek spritu, napriklad pre kazdy square
                this.addEventListener(MouseEvent.CLICK, this.click);
                this.addEventListener(MouseEvent.MOVE, this.move);
    
            }
            CircuitLevel.prototype = new Sprite(); // toto musi byt  
            CircuitLevel.prototype = new Level();
            
     
       
        </script>
        <script type="text/javascript" src="level.js"></script>
    </body>
</html>
-->